package com.fleet.project01.adapter;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.fleet.project01.R;
import com.fleet.project01.data.model.Repository;

import java.util.ArrayList;
import java.util.List;

public class RepositoryAdapter extends RecyclerView.Adapter<RepositoryAdapter.RepositoryViewHolder> {
    private List<Repository> repositories;
    private OnItemClickListener onItemClickListener;

    public interface OnItemClickListener {
        void onItemClick(Repository repository);
    }

    public RepositoryAdapter() {
        this.repositories = new ArrayList<>();
    }

    public void setRepositories(List<Repository> repositories) {
        this.repositories = repositories != null ? repositories : new ArrayList<>();
        notifyDataSetChanged();
    }

    public void setOnItemClickListener(OnItemClickListener listener) {
        this.onItemClickListener = listener;
    }

    @NonNull
    @Override
    public RepositoryViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.item_repository, parent, false);
        return new RepositoryViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull RepositoryViewHolder holder, int position) {
        Repository repository = repositories.get(position);
        holder.bind(repository);
    }

    @Override
    public int getItemCount() {
        return repositories.size();
    }

    class RepositoryViewHolder extends RecyclerView.ViewHolder {
        private TextView tvRepoName;
        private TextView tvRepoDescription;
        private TextView tvStarCount;
        private TextView tvForkCount;
        private TextView tvLanguage;

        public RepositoryViewHolder(@NonNull View itemView) {
            super(itemView);
            tvRepoName = itemView.findViewById(R.id.tvRepoName);
            tvRepoDescription = itemView.findViewById(R.id.tvRepoDescription);
            tvStarCount = itemView.findViewById(R.id.tvStarCount);
            tvForkCount = itemView.findViewById(R.id.tvForkCount);
            tvLanguage = itemView.findViewById(R.id.tvLanguage);

            itemView.setOnClickListener(v -> {
                if (onItemClickListener != null) {
                    int position = getAdapterPosition();
                    if (position != RecyclerView.NO_POSITION) {
                        onItemClickListener.onItemClick(repositories.get(position));
                    }
                }
            });
        }

        public void bind(Repository repository) {
            tvRepoName.setText(repository.getName());

            // 设置描述，如果为空则显示默认文本
            String description = repository.getDescription();
            if (description == null || description.trim().isEmpty()) {
                tvRepoDescription.setText("暂无描述");
            } else {
                tvRepoDescription.setText(description);
            }

            // 设置星标数量
            tvStarCount.setText("⭐ " + repository.getStarCount());

            // 设置分叉数量
            tvForkCount.setText("🍴 " + repository.getForkCount());

            // 设置编程语言
            String language = repository.getLanguage();
            if (language == null || language.trim().isEmpty()) {
                tvLanguage.setText("未知语言");
            } else {
                tvLanguage.setText(language);
            }
        }
    }
}
